package src.com.qf.controller;

import src.com.qf.entity.Address;
import src.com.qf.entity.GoodCar;
import src.com.qf.entity.User;
import src.com.qf.service.AddressService;
import src.com.qf.service.GoodCarService;
import src.com.qf.service.impl.AddressServiceImpl;
import src.com.qf.service.impl.GoodCarServiceImpl;

import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.math.BigDecimal;
import java.util.List;

@WebServlet("/car")
public class GoodCarController extends BaseController {
    private GoodCarService service = new GoodCarServiceImpl();
    private AddressService addressService = new AddressServiceImpl();
    public String addCart(HttpServletRequest request, HttpServletResponse response){
        String strGoodId = request.getParameter("goodId");
        String strPrice = request.getParameter("price");

        GoodCar car = new GoodCar();
        car.setGoodId(Integer.parseInt(strGoodId));
        //默认添加一个商品
        car.setGoodNum(1);
        //小计应为价格*数量，但数量默认为1，则省略
        car.setGoodSum(new BigDecimal(strPrice));
        User user = (User) request.getSession().getAttribute("loginUser");
        car.setUid(user.getId());

        int result = service.addCart(car);
        if (result == 1){
            System.out.println("添加成功");
            return "redirect:cartSuccess.jsp";
        }else {
            request.setAttribute("msg","添加购物车失败");
            return "forward:error/error.jsp";
        }
    }

    /**
     * 查询用户所有购物车信息
     * @param request
     * @param response
     * @return
     */
    public String getCarts(HttpServletRequest request, HttpServletResponse response){

        User user = (User) request.getSession().getAttribute("loginUser");

        if (user == null){
            return "forward:login.jsp";
        }

        List<GoodCar> list = service.getCarts(user.getId());

        request.setAttribute("carts",list);
        return "forward:cart.jsp";
    }

    /**
     * 增加或减少购物车中商品数量的方法
     * @param request
     * @param response
     * @return
     */
    public String updateCartNum(HttpServletRequest request, HttpServletResponse response){

        String strGoodId = request.getParameter("pid");
        String strPrice = request.getParameter("price");
        //num =0 时为删除整条数据，为1时增加一个数量，-1时减少
        String strNum = request.getParameter("num");
        User user = (User) request.getSession().getAttribute("loginUser");

        int result = service.updateCartNum(Integer.parseInt(strGoodId),new BigDecimal(strPrice),Integer.parseInt(strNum),user.getId());
        return String.valueOf(result);
    }

    /**
     * 删除购物车的方法
     * @param request
     * @param response
     * @return
     */
    public String clearCart(HttpServletRequest request, HttpServletResponse response){
        String strId = request.getParameter("id");
        int result = service.clearCart(Integer.parseInt(strId));
        if(result == 0){
            request.setAttribute("msg","删除失败");
            return "forward:error/error.jsp";
        }else {
            return "redirect:car?action=getCarts";
        }
    }

    /**
     * 清空购物车
     * @param request
     * @param response
     * @return
     */
    public String clearAllCar(HttpServletRequest request, HttpServletResponse response){
        User user = (User) request.getSession().getAttribute("loginUser");
        int result = service.clearAllCar(user.getId());
        if(result == 0){
            request.setAttribute("msg","清除失败");
            return "forward:error/error.jsp";
        }else {
            return "redirect:good?action=getGoodsListByTypeId";
        }
    }

    /**
     * 生成订单预览界面
     * @param request
     * @param response
     * @return
     */
    public String getOrderView(HttpServletRequest request, HttpServletResponse response){
        User user = (User) request.getSession().getAttribute("loginUser");
        //根据用户id获得该用户的购物车信息
        List<GoodCar> carts = service.getCarts(user.getId());
        request.setAttribute("cartList",carts);

        //查询该人员的地址信息列表
        List<Address> list = addressService.getAddressList(user.getId());
        request.setAttribute("addList",list);

        return "forward:order.jsp";
    }
}
